Boolean Expressions |
Objectives |
Discussion |
Boolean Expressions
What are the Values of True and False?
Dim x As Boolean
messagebox.show(cint(x))
x = True
messagebox.show(cint(x))
x = False
messagebox.show(cint(x))
If...then
if (a < b) then messagebox.show("True")
or...
if (a < b) then
messagebox.show("True")
end if
if x then messagebox.show("True")
MessageBox
Enumerations
Dim x As DialogResult
x = Messagebox.show(...)
if x = DialogResult.Yes then ...
Demonstration |
1. Open your Unit3 project and add a new form (frmBoolean).
2. Add a button (btnCheck).
3. Create a sub to check Boolean expressions:
Private Sub CheckBoolean()
messagebox.show(3 < 4 and 5 >= 6)
messagebox.show(3 < 4 or 5 >= 6)
messagebox.show(not(3 < 4 and 5 >= 6))
messagebox.show(3 <> 4 and 5 >= 6)
messagebox.show(5 < 4 < 6)
messagebox.show("a" = "A")
messagebox.show(3 < 4 and 5 >= 6 or 5 < 4)
End Sub
4. Call this sub inside the click event for the button. Then run the program and check each of the answers. Experiment with your own expressions until you can determine if the expression will yield True or False.
5. Now we will experiment with if...then. We will create a function that will compare three numbers to determine if the numbers are in order from lowest to highest:
Private Function InOrder _
(a as int16, b as int16, c as int16) _
as Boolean
if (a < b and b < c) then
return(True)
end if
return(False)
End Function
6. In the click event for the button, comment out the call to the Sub. Add the following two lines which call the function:
MessageBox.Show(InOrder(3, 5, 7))
MessageBox.Show(InOrder(3, 2, 5))
7. Test the code. Then experiment by passing other numbers into the function InOrder.
8. We will demonstrate another simple example using a messagebox. Add a button (btnEnd) to your form and set its text property to "Exit". We will use this button to end the program; but we will confirm that the user really wants to exit. Add the following code to the click event for the button:
Dim answer As DialogResult
answer = MessageBox.Show("Do you want to exit", _
"Exit", MessageBoxButtons.YesNo, _
MessageBoxIcon.Question)
If answer = DialogResult.Yes Then
End
End If
9. If you did not type in the code for the messagebox.show statement then you should. As you type you will be prompted for the next item after each comma and you will be provided with lists of choices. Notice how many different things you can do with a messagebox. Also notice that we declared "answer" as DialogResult. DialogResult is a special enumerated type that can have only predefined values. When you type in the line "if answer = " you are provided with a list of acceptable answers based on the type being a DialogResult. Finally, notice that "End" causes the program to end.
10. Test your code. You should be asked if you want to exit. If you choose "yes" then the program should end; otherwise it won't.
Exercises |
1. Given the following: x = 5, y = 10, z = 20, sName = "bo". Review each of the following statements and determine if they will evaluate to True or False:
2. Create a function that accepts 3 numbers, determines if the numbers are in reverse order from highest to lowest, and returns True or False. Write a program to test your function.
3. Create a program that will demonstrate the AND and the OR operators. Your program should meet the following requirements:
Links & Help |